﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.IO;
using System.Drawing.Imaging;
using System.Drawing;


public partial class Application_Controls_Utils_thumbnail : System.Web.UI.Page
{

	string imagePath;
	int width=-1, height=-1;

    protected void Page_Load(object sender, EventArgs e)
    {
		if(!string.IsNullOrEmpty(Request.QueryString["w"]))
			width = Convert.ToInt32(Request.QueryString["w"]);

		if(!string.IsNullOrEmpty(Request.QueryString["width"]))
			width = Convert.ToInt32(Request.QueryString["width"]);

		if(!string.IsNullOrEmpty(Request.QueryString["h"]))
			height = Convert.ToInt32(Request.QueryString["h"]);

		if(!string.IsNullOrEmpty(Request.QueryString["height"]))
			height = Convert.ToInt32(Request.QueryString["height"]);

		//Setting default
		if(height==-1)
			height=20;

		imagePath = Server.MapPath(Request.QueryString["image"]);

		System.Drawing.Image img = System.Drawing.Image.FromFile(imagePath);
		int newWidth = img.Width*height/img.Height;
		System.Drawing.Image thumb = img.GetThumbnailImage(newWidth,height,null,IntPtr.Zero);

		Response.Clear();

		if (img.RawFormat == ImageFormat.Bmp)
			Response.ContentType = "image/bmp";
		else if (img.RawFormat == ImageFormat.Gif)
			Response.ContentType = "image/gif";
		else if (img.RawFormat == ImageFormat.Jpeg)
			Response.ContentType = "image/jpeg";
		else if (img.RawFormat == ImageFormat.Png)
			Response.ContentType = "image/png";
		else
			Response.ContentType = "application/octet-stream";
		
		img.Save(Response.OutputStream, img.RawFormat);
    }
}
